rm(list=ls())


library(foreign)
library(ggplot2)
library(dplyr)
library(rworldmap)
library(countrycode)
library(rstudioapi)

setwd(dirname(getActiveDocumentContext()$path))

sink("NCA_Stats_log.txt")

#1. Nuclear safety: Agreements that authorize cooperation in the field of nuclear safety.
#2. Intangibles: Treaties dealing primarily with cooperation in research and development or training.
#3. Nuclear materials: Deals pertaining to the transfer of nuclear materials, like uranium, heavy water, or plutonium.
#4. Research: Agreements that authorize cooperation in the development of a
#nuclear program for research. These treaties typically call for the export of
#research reactors, but may include other areas as well.
#5. Comprehensive power – restricted: Treaties that call for cooperation in the
#development of a nuclear program for electricity production. These deals prohibit
#enrichment and reprocessing assistance, making them stronger from a nonproliferation standpoint.
#6. Comprehensive power – unrestricted: Deals that place no explicit prohibitions
#on cooperation in enrichment and reprocessing technology.
#7. Military assistance: Agreements that authorize nuclear assistance explicitly
#to help the recipient build nuclear weapons. These deals could include any type
#of cooperation, as long as their purpose is for bomb development.


mydata <- read.dta("nca_dataset.dta")
summary(mydata$year)
table(mydata$ccode1)

NCA_data <- matrix(NA, nrow = 6, ncol = 2)
NCA_data[1,1] = "USA"
NCA_data[2,1] = "RUS"
NCA_data[3,1] = "UKG"
NCA_data[4,1] = "FRA"
NCA_data[5,1] = "CHN"
NCA_data[6,1] = "Others"

NCA_data[1,2] = 630
NCA_data[2,2] = 312
NCA_data[3,2] = 199
NCA_data[4,2] = 360
NCA_data[5,2] = 58
NCA_data[6,2] = 1593


data <- data.frame(
  Countries=c("USA","RUS","UKG","FRA","CHN", "Others") ,  
  Agreements=c(630, 312, 199, 360, 58, 1593)
)


# Figure 2
ggplot(data, aes(x=Countries, y=Agreements)) + 
  geom_bar(stat = "identity") + theme(axis.text = element_text(size = 20)) +
  theme(axis.title.x = element_text(size = 20)) + theme(axis.title.y = element_text(size = 20))



# Figure 3
mydata_map <- mydata
mydata_map <- subset(mydata_map, mydata_map$ccode1 == 2) # US only
mydata_map <- dplyr::select(mydata_map, ccode2, country2, year, nca)
mydata_map$iso3 <- countrycode(mydata_map$ccode2, origin = "cown", destination = "iso3c")
mydata_map$iso3 <- ifelse(mydata_map$ccode == 260, "DEU", mydata_map$iso3)
mydata_map$iso3 <- ifelse(mydata_map$ccode == 315, "CZE", mydata_map$iso3)
mydata_map$iso3 <- ifelse(mydata_map$ccode == 345, "SRB", mydata_map$iso3)
mydata_map <- mydata_map %>% group_by(ccode2) %>% mutate(country_total = sum(nca))
mydata_map <- mydata_map %>% dplyr::distinct(ccode2, .keep_all = TRUE)
data = mydata_map
sPDF <- joinCountryData2Map(data, joinCode = "ISO3", nameJoinColumn = "iso3")
mapParams = mapCountryData(sPDF, nameColumnToPlot="country_total",catMethod="categorical",mapTitle="", addLegend=FALSE,colourPalette=c('yellow'))
do.call(addMapLegend, c(mapParams
                        ,legendLabels="all"
                        ,legendWidth=0.5
                        ,legendIntervals="data"
))


sink()
